package com.lishe.lishe.lishe.module.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.base.BaseBean;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.mall.bean.CouponObject;
import com.lishe.lishe.lishe.module.mall.http.HttpMall;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ToastUtil;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.LinkedList;

/**
 * Created by Bin
 */
public class CouponListAdapter extends RecyclerviewBaseAdapter {

    private Context mContext;
    private LinkedList<CouponObject> mCouponList;

    public CouponListAdapter(Context context, LinkedList<CouponObject> list) {
        this.mCouponList = list;
        this.mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goodsdetail_coupon, null, false);
        ItemViewHolder holder = new ItemViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.date.setText(mCouponList.get(position).getCoupon_use_start_date() + "-" + mCouponList.get(position).getCoupon_use_end_date());
        switch (mCouponList.get(position).getCoupon_type()) {
            case "1":
                viewHolder.couponType.setText("平台");
                break;
            case "2":
                viewHolder.couponType.setText("店铺");
                break;
            case "3":
                viewHolder.couponType.setText("企业");
                break;
            case "4":
                viewHolder.couponType.setText("活动");
                break;
        }
        //1：未领取，2：未使用，3：已使用，4：已过期，5：已抢光
        switch (mCouponList.get(position).getCoupon_use_status()) {
            case "1"://未领取
                viewHolder.use.setText("点击领取");
                viewHolder.use.setBackgroundResource(R.drawable.bg_red_corner_15dp);
                viewHolder.use.setTextColor(mContext.getResources().getColor(R.color.ff7972));
                break;
            case "2"://未使用
                viewHolder.use.setText("已领取");
                viewHolder.use.setBackgroundResource(R.drawable.bg_15dp_corner_999999_solid);
                viewHolder.use.setTextColor(mContext.getResources().getColor(R.color.white));
                break;
            case "3"://已使用
                viewHolder.use.setText("已使用");
                viewHolder.use.setBackgroundResource(R.drawable.bg_15dp_corner_999999_solid);
                viewHolder.use.setTextColor(mContext.getResources().getColor(R.color.white));
                break;
            case "4"://已过期
                viewHolder.use.setText("已过期");
                viewHolder.use.setBackgroundResource(R.drawable.bg_15dp_corner_999999_solid);
                viewHolder.use.setTextColor(mContext.getResources().getColor(R.color.white));
                break;
            case "5"://已抢光
                viewHolder.use.setText("已抢光");
                viewHolder.use.setBackgroundResource(R.drawable.bg_15dp_corner_999999_solid);
                viewHolder.use.setTextColor(mContext.getResources().getColor(R.color.white));
                break;
        }
        viewHolder.use.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(mCouponList.get(position).getCoupon_use_status())) {
                    getCoupon(mCouponList.get(position).getCoupon_id(), position);
                }
            }
        });
        viewHolder.couponName.setText(mCouponList.get(position).getCoupon_type_detail());
        switch (mCouponList.get(position).getCoupon_rule()) {  //优惠券规则
            //1：满减，2：满折，3：满免邮，4：直减
            case "1":
                viewHolder.pricelayout.setVisibility(View.VISIBLE);
                viewHolder.price.setText(mCouponList.get(position).getCoupon_decrease());
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                break;
            case "2":
                viewHolder.pricelayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                viewHolder.discountlayout.setVisibility(View.VISIBLE);
                viewHolder.discount.setText(mCouponList.get(position).getCoupon_discount());
                break;
            case "3":
                viewHolder.pricelayout.setVisibility(View.GONE);
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.VISIBLE);
                break;
            case "4":
                viewHolder.pricelayout.setVisibility(View.VISIBLE);
                viewHolder.price.setText(mCouponList.get(position).getCoupon_decrease());
                viewHolder.discountlayout.setVisibility(View.GONE);
                viewHolder.freightlayout.setVisibility(View.GONE);
                break;
        }
        viewHolder.condition.setText(mCouponList.get(position).getCoupon_rule_detail());
    }

    /**
     * 领取优惠券
     */
    private void getCoupon(String couponId, final int position) {
        LoadingUtils.createLoadingDialog(mContext, "加载中...", true).show();
        HttpMall.getCoupon(couponId, Globals.getToken(), new DataResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean bean) {
                LoadingUtils.closeDialog();
                ToastUtil.showToast(mContext, bean.getMsg());
                mCouponList.get(position).setCoupon_use_status("2");
                notifyDataSetChanged();
            }

            @Override
            public void onFail(String failMessage) {
                LoadingUtils.closeDialog();
                ToastUtil.showToast(mContext, failMessage);
            }

            @Override
            public void onError(Throwable e) {
                LoadingUtils.closeDialog();
                ToastUtil.showToast(mContext, R.string.network_or_server_exception);
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return mCouponList == null ? 0 : mCouponList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView price;
        TextView condition;
        TextView couponType;
        TextView couponName;
        TextView date;
        TextView use;
        View pricelayout;
        View discountlayout;
        View freightlayout;
        TextView discount;

        public ItemViewHolder(View v) {
            super(v);
            price = v.findViewById(R.id.price);
            condition = v.findViewById(R.id.condition);
            couponType = v.findViewById(R.id.couponType);
            couponName = v.findViewById(R.id.couponName);
            date = v.findViewById(R.id.date);
            use = v.findViewById(R.id.use);
            pricelayout = v.findViewById(R.id.pricelayout);
            discountlayout = v.findViewById(R.id.discountlayout);
            freightlayout = v.findViewById(R.id.freightlayout);
            discount = v.findViewById(R.id.discount);
        }
    }


}
